package com.vivo.Utils;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class HttpUtil {
    private static CloseableHttpClient httpClient;

    static {
        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
        cm.setMaxTotal(20);
        cm.setDefaultMaxPerRoute(5);
        httpClient = HttpClients.custom().setConnectionManager(cm).build();
    }

//    public static int get(String url, String dir, String name) {
//        int imgSize = 0;
//        try {
//            HttpGet httpGet = new HttpGet(url);
//            HttpResponse response = httpClient.execute(httpGet);
//            InputStream input = response.getEntity().getContent();
//            imgSize = (int)response.getEntity().getContentLength();
//
//            File file = new File(dir);
//            if (!file.exists() && !file.isDirectory()) {
//                file.mkdirs();
//                System.out.println("create direct :" + dir);
//            }
//
//            String filePath = dir + name;
//            File file1 = new File(filePath);
//            if (!file1.exists() && response.getStatusLine().getStatusCode() == 200) {
//                FileOutputStream fos = new FileOutputStream(filePath);
//                byte[] data = new byte[1024 * 100];
//                int len = 0;
//                while ((len = input.read(data)) != -1) {
//                    fos.write(data, 0, len);
//                }
//                fos.close();
//
//                System.out.println(filePath);
//            }
//
//            input.close();
//
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//
//        return imgSize;
//    }

    public static String get(String url) {
        int imgSize = 0;
        String retStr = "";
        try {
            HttpGet httpGet = new HttpGet(url);
            HttpResponse response = httpClient.execute(httpGet);
            InputStream input = response.getEntity().getContent();

            retStr = EntityUtils.toString(response.getEntity(), "UTF-8");

            if (response.getStatusLine().getStatusCode() == 200) {

            }

            System.out.println(retStr);

            input.close();

        } catch (IOException e) {
            e.printStackTrace();
        }

        return retStr;
    }

    /**
     * put method
     * @param url put dest url
     * @param xmlObj xml body
     * @return 0 if success, or -1
     */
    public static int put(String url, String xmlObj) {
        String retStr = null;
        Integer retVal = -1;
        HttpPut httpPut = new HttpPut(url);
        httpPut.addHeader("Content-Type", "application/xml");
        StringEntity entity = null;
        try {
            entity = new StringEntity(xmlObj, "UTF-8");
            httpPut.setEntity(entity);
            HttpResponse response = httpClient.execute(httpPut);
            retStr = EntityUtils.toString(response.getEntity(), "UTF-8");
            if (response.getStatusLine().getStatusCode() == 200) {
                retVal = 0;
            }

            System.out.println(retStr);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return retVal;
    }


    public static String post(String url, Map<String, String> paramPairs) {
        String result = null;
        HttpPost httpPost = new HttpPost(url);
        ArrayList nameValuePairs = new ArrayList(paramPairs.size());
        for (String key : paramPairs.keySet()) {
            nameValuePairs.add(new BasicNameValuePair(key, paramPairs.get(key)));
            System.out.println("Key : " + key + " - Value : " + paramPairs.get(key));
        }

        try {
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
            HttpResponse response = httpClient.execute(httpPost);
            int statusCode = response.getStatusLine().getStatusCode();
            System.out.println(statusCode);
            if(statusCode == 200){
                result = EntityUtils.toString(response.getEntity());
                System.out.println(result);
                return result;
            }else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

//    PostMethod method = new PostMethod("/Default/ReduceMemberRecord");
//
//
//
//    NameValuePair waresIdValue = new NameValuePair("waresid",waresid);
//    NameValuePair wareshousenameValue = null;
//
//    wareshousenameValue = new NameValuePair("warehousename", warehousename);
//
//    NameValuePair countValue = new NameValuePair("count",count);
//    NameValuePair scoreValue = new NameValuePair("score",score);
//    NameValuePair memberCodeValue = new NameValuePair("memberCode",memberCode);

//    ("1176",warehousename,"1","10","18811917740")

    public static void main(String[] args) {
        HashMap paramMap = new HashMap();
        paramMap.put("waresid", "1176");
        paramMap.put("warehousename", "江阴马镇北渚手机店仓库");
        paramMap.put("count", "1");
        paramMap.put("score", "10");
        paramMap.put("memberCode", "18811917740");

//        HttpUtil.post("http://ahapi.vivo.xyz:8888/Default/ReduceMemberRecord", paramMap);
        HttpUtil.post("http://ahapi.vivo.xyz:8888/Default/ReduceMemberRecord", paramMap);

    }
}